package entity;

import org.apache.commons.collections.map.HashedMap;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/22.
 */
public class ShopCartEntity implements Serializable {
    /**
     * key:商品的id
     * value:商品对象
     */
    private Map<Long,GoodsEntity> map=new HashedMap();
    //加入购物车
    public void add(GoodsEntity ge){
        if(map.containsKey(ge.getId())){
         GoodsEntity goods=map.get(ge.getId());
            goods.setCounts(goods.getCounts()+1);
        }else{
            ge.setCounts(1);
            map.put(ge.getId(),ge);
        }
    }
    //移除商品
    public void remove(Integer goodsId){
        map.remove(goodsId);
    }

    /**
     * 修改数量
     * @param goodsId
     * @param count
     */
    public void updateCount(Integer goodsId,Integer count){
        if(map.containsKey(goodsId)){
            if(count>0){
                map.get(goodsId).setCounts(count);
            }
        }
    }
    /**
     * 购物车中所有的商品
     * @return
     */
    public Collection<GoodsEntity> getGoods(){
       return map.values();
    }
    /**
     * 计算总价格
     * @return
     */
    public Double getTotalPrice(){
        double total=0F;
        for (GoodsEntity goods :getGoods()){
            total+=goods.getPrice()*goods.getCounts();
        }
        return total;
    }

}
